package org.ssmd4.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ssmd4.pojo.Ebook;
import org.ssmd4.pojo.EbookType;
import org.ssmd4.service.EbookService;
import org.ssmd4.service.EbookTypeService;
import org.ssmd4.util.PageTool;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/ebook")
public class EbookController {

    @Resource
    EbookTypeService ebookTypeService;
    @Resource
    EbookService ebookService;


    @RequestMapping("/list")
    public String list(Model model, Ebook ebook){

        int totalRecordCount=ebookService.getRecordCount(ebook);
        PageTool pageTool=new PageTool(totalRecordCount,ebook.getCurrPageNo(),5);
        int startNum=pageTool.getStartRow();
        int pageSize=pageTool.getPageSize();
        ebook.setStarNum(startNum);
        ebook.setPageSize(pageSize);

        List<Ebook> list=ebookService.getEbookListByPage(ebook);
        List<EbookType> plist=ebookTypeService.getAllEbookTypeList();

        model.addAttribute("list",list);
        model.addAttribute("plist",plist);
        model.addAttribute("ebook",ebook);
        model.addAttribute("pageTool",pageTool);

        model.addAttribute("prePages",pageTool.getPrePages());
        model.addAttribute("nextPages",pageTool.getNextPages());

        return "/ebook/list";
    }

    @RequestMapping("/save")
    public String save(Model model){
        List<EbookType> plist=ebookTypeService.getAllEbookTypeList();
        model.addAttribute("plist",plist);

        return "/ebook/save";
    }

    @RequestMapping("/dosave")
    public String doSave(Model model,  Ebook ebook){
//        if (ebookService.save(ebook)>0){
//            model.addAttribute("label","alert('新增数据成功');");
//        }
        List<EbookType> plist=ebookTypeService.getAllEbookTypeList();
        model.addAttribute("plist",plist);
        return "/ebook/save";
    }


    @RequestMapping("/dodelete")
    @ResponseBody
    public String doDelete(Model model,int id){
        try {
            ebookService.delete(id);
            return "1";
        }catch (Exception e){
            return "0";
        }

    }

    @RequestMapping("/dodeleteall")
    @ResponseBody
    public String doDeleteAll(Model model,String[] ids){
        try {
            ebookService.deleteAll(ids);
            return "1";
        }catch (Exception e){
            return "0";
        }

    }


    @RequestMapping("/update")
    public String update(Model model,Integer id){
        Ebook ebook =ebookService.getOneEbook(id);
        List<EbookType> plist=ebookTypeService.getAllEbookTypeList();
        model.addAttribute("plist",plist);
        model.addAttribute("ebook",ebook);
        return "/ebook/update";
    }

    @RequestMapping("/doupdate")
    public String doUpdate(Model model,  Ebook ebook){
//        if (ebookService.update(ebook)>0){
//            model.addAttribute("label","alert('修改数据成功');");
//        }

        List<EbookType> plist=ebookTypeService.getAllEbookTypeList();
        model.addAttribute("plist",plist);
        model.addAttribute("ebook",ebook);



        ebookService.update(ebook);
        return "redirect:/ebook/list";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public String editNews(String title,String summary,String uploaduser,Integer id){

        try {
            Ebook ebook=ebookService.getOneEbook(id);
            ebook.setTitle(title);
            ebook.setSummary(summary);
            ebook.setUploaduser(uploaduser);
            ebookService.update(ebook);
            return "1";
        }catch (Exception e){
            e.printStackTrace();
        }

        return "0";
    }

}
